package com.chenglin.picturepicker.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chenglin.picturepicker.bean.MediaBean
import com.chenglin.picturepicker.R
import com.chenglin.picturepicker.databinding.AdapterItemBinding
import com.chenglin.picturepicker.databinding.SelectPhotoTopBinding


class SelectPhotoAdapter(var context: Context, var topList: List<MediaBean>, var bottomList: List<MediaBean>,var callBack : SelectPhoneCallBack) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    private val photo_top_type = 100 //头部

    private val photo_bottom_type = 101 //底部
    private var selectList = arrayListOf<MediaBean>()  //已经选择图片


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == photo_top_type) {
           var selectPhotoTopBinding = SelectPhotoTopBinding.inflate(LayoutInflater.from(context), parent, false)
            return TopImageHolder(selectPhotoTopBinding)
        } else {
            var imageBinding = AdapterItemBinding.inflate(LayoutInflater.from(context), parent, false)
            return ImageHolder(imageBinding)
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is TopImageHolder){
            holder.topHolder.rvTop.layoutManager = GridLayoutManager(context,2)
            holder.topHolder.rvTop.adapter = ImageAdapter(context,topList,object : ImageAdapter.ImageAdapterCallBack{
                override fun onClickTag(position: Int, bean: MediaBean) {
                    holder.topHolder.rvTop.adapter?.notifyItemChanged(position)
                    if (bean.isSelect){
                        selectList.add(bean)
                    }else{
                        if (selectList.contains(bean)){
                            selectList.remove(bean)
                        }else{

                        }
                    }
                    callBack.onGetList(selectList)
                }

                override fun onClickItem(position: Int, bean: MediaBean) {
                   callBack.onScanBigImage(bean.path)
                }

            })
            holder.topHolder.ivCamera.setOnClickListener {
                callBack.onOpenCamera()
            }
        }else if (holder is ImageHolder){
            if (bottomList[position-1].isSelect){
                holder.imageHolder.ivSelectTag.setBackgroundResource(R.drawable.image_selecor_selected)
            }else{
                holder.imageHolder.ivSelectTag.setBackgroundResource(R.drawable.image_selector_unselected)
            }
            Glide.with(context).load(bottomList[position-1].path).error(R.drawable.image_failure).override(200,200).into( holder.imageHolder.ivSelect)

            holder.imageHolder.ivSelectTag.setOnClickListener {
                bottomList[position-1].isSelect = !bottomList[position-1].isSelect
                if (bottomList[position-1].isSelect){
                    selectList.add(bottomList[position-1])
                }else{
                    if (selectList.contains(bottomList[position-1])){
                        selectList.remove(bottomList[position-1])
                    }else{

                    }
                }
                callBack.onSelectPhotoTag(position)
                callBack.onGetList(selectList)
            }

            holder.imageHolder.ivSelect.setOnClickListener {
                callBack.onScanBigImage(bottomList[position-1].path)
            }
        }
    }


    override fun getItemViewType(position: Int): Int {
        if (position == 0){
            return photo_top_type
        }else{
            return photo_bottom_type
        }
    }

    override fun getItemCount(): Int {
        return bottomList.size+1
    }

    class TopImageHolder( var topHolder: SelectPhotoTopBinding) : RecyclerView.ViewHolder(topHolder.root) {

    }

    class ImageHolder(var imageHolder: AdapterItemBinding) : RecyclerView.ViewHolder(imageHolder.root) {

    }

    interface SelectPhoneCallBack{
        fun onOpenCamera()
        fun onScanBigImage(path:String)
        fun onSelectPhotoTag(position: Int)
        fun onGetList(selectList :List<MediaBean>);

    }





}